package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.api.HuanXinApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UsersApi;
import com.tanhua.dubbo.pojo.HuanXinUser;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.pojo.Users;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.UserInfoVo;
import com.tanhua.server.vo.UsersVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IMService {

    @DubboReference(version = "1.0.0")
    private HuanXinApi huanXinApi;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @DubboReference(version = "1.0.0")
    private UsersApi usersApi;

    public UserInfoVo queryUserInfoByUserName(String userName) {
        HuanXinUser huanXinUser = this.huanXinApi.queryUserByUserName(userName);
        if (null == huanXinUser) {
            return null;
        }
        UserInfo userInfo = this.userInfoApi.queryByUserId(huanXinUser.getUserId());
        UserInfoVo userInfoVo = BeanUtil.toBeanIgnoreError(userInfo, UserInfoVo.class);
        userInfoVo.setGender(userInfo.getSex().name().toLowerCase());
        userInfoVo.setMarriage(StrUtil.equals("已婚", userInfo.getMarriage()) ? 1 : 0);
        return userInfoVo;
    }

    public Object addContacts(Long friendId) {
        Long userId = UserThreadLocal.get();
        //将好友关系写入到MongoDB
        String result = this.usersApi.saveUsers(userId, friendId);
        if (StrUtil.isEmpty(result)) {
            return ErrorResult.builder()
                    .errCode("5001")
                    .errMessage("添加好友好像到MongoDB失败！")
                    .build();
        }

        //将好友关系写入到环信
        Boolean hxResult = this.huanXinApi.addUserFriend(userId, friendId);
        if (!hxResult) {
            return ErrorResult.builder()
                    .errCode("5002")
                    .errMessage("添加好友关系到环信失败！")
                    .build();
        }

        return null;
    }

    public PageResult queryContactsList(Integer page, Integer pageSize, String keyword) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        Long userId = UserThreadLocal.get();
        List<UserInfo> friendUserInfoList;
        if (StrUtil.isEmpty(keyword)) {
            //分页查询
            PageInfo<Users> pageInfo = this.usersApi.queryUsersList(userId, page, pageSize);
            List<Object> friendIdList = CollUtil.getFieldValues(pageInfo.getRecords(), "friendId");
            friendUserInfoList = this.userInfoApi.queryByUserIdList(friendIdList);
        } else {
            //按照用户名like查询
            List<Users> usersList = this.usersApi.queryAllUsersList(userId);
            List<Object> friendIdList = CollUtil.getFieldValues(usersList, "friendId");
            friendUserInfoList = this.userInfoApi.queryLikeUserName(friendIdList, keyword);
        }

        List<UsersVo> usersVoList = new ArrayList<>();

        friendUserInfoList.forEach(userInfo -> {
            UsersVo usersVo = BeanUtil.toBeanIgnoreError(userInfo, UsersVo.class);
            usersVo.setUserId("HX_" + userInfo.getUserId());
            usersVo.setGender(userInfo.getSex().name().toLowerCase());
            usersVo.setCity(StrUtil.subBefore(userInfo.getCity(), '-', false));
            usersVoList.add(usersVo);
        });

        pageResult.setItems(usersVoList);
        return pageResult;
    }
}
